<?php
/**
 * 我的商品
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Goods;

use App\Admin\Actions\NetworkGoods\GoodsAudit;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;




class ReviewController extends Controller
{
    use ModelForm;

    protected $header = '商品审核';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function grid(){

        $grid = new Grid(new NetworkGoodsModel());

        $grid->model()->where(['review_status'=>0])->orderBy('created_at', 'DESC');

        $grid->column('Network.name','网点名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('Goods.id','商品编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('Goods.goods_title','商品标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('Goods.goods_cover', '封面')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('Goods.goods_type','类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : GoodsTypeModel::selectValue('id',$filed);
        });

        $grid->column('Goods.goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('Goods.goods_price','单价')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('goods_stock','库存')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('goods_sales','销量')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('fictitious_goods_sales','虚拟销量')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('is_fictitious','是否开启虚拟销量')->using(DataDictionaryModel::Whether, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');


        $grid->column('status','状态')->using(DataDictionaryModel::GoodsStatus, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('review_status','审核状态')->using(DataDictionaryModel::AuditStatus, '未知')->dot([
            0 => 'danger',
            1 => 'info',
            2 => 'warning'
        ], 'success');

        $grid->column('goods_audit_records', '审核记录')->expand(function ($model) {
            $comments = $model->NetworkGoodsReview()->get()->map(function ($comment) {
                return $comment->only(['status_name','content']);
            });
            return new Table(['审核状态','原因'], $comments->toArray());
        })->help('点击下方查看对应的审核记录');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        $grid->column('update_at','审核')->display(function ($id, $column) use ($grid) {

            if($this->review_status == 0){
                $action = new GoodsAudit();
                return $action
                    ->asColumn()
                    ->setGrid($grid)
                    ->setColumn($column)
                    ->setRow($this);
            }


        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('Goods.goods_title','商品名称');
                $filter->like('Network.names','网点名称');
                $filter->between('created_at', '创建时间')->datetime();

            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
                $filter->equal('review_status', '审核状态')->select(DataDictionaryModel::AuditStatus);
                $filter->equal('Goods.goods_type', '类型')->select(GoodsTypeModel::selectOptions());
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->disableActions();

        $grid->fixColumns(1, -1);

        $grid->disableCreateButton();

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new NetworkGoodsModel());

        $form->number('fictitious_goods_sales', '虚拟库存');

        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('is_fictitious', '是否开启虚拟库存')->states($states);


        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }


}